<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="resetFormData">
        绑定菜单
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      row-key="id"
      border
    >
      <el-table-column
        prop="menuName"
        label="菜单名称"
      />
      <el-table-column label="操作" width="140" align="center">
        <template slot-scope="{row}">
          <el-button type="danger" @click="deleteBindInfo(row.id)">解除绑定 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" style="width: 1100px;margin: auto">
      <FlowMenuList @selectMenuData="selectMenuData" />
    </el-dialog>
  </div>
</template>
<script>
import { SaveFlowBindInfo, GetFlowBindInfoList, DeleteFlowBindInfo } from '@/api/SystemManagerApi'
import FlowMenuList from '@/views/SystemSetting/SystemFlow/components/FlowMenuList'

const optionsMap = {
  'START': '开始节点',
  'NODE': '审批节点',
  'END': '结束节点',
  'FLOW': '子流程'
}

export default {
  components: { FlowMenuList },
  filters: {
    exchange(val) {
      return optionsMap[val]
    }
  },
  props: ['flowId'],
  data() {
    return {
      rules: {
        sort: [{ required: true, message: 'type is required', trigger: 'change' }],
        name: [{ required: true, message: 'type is required', trigger: 'change' }]
      },
      title: '编辑节点',
      fromTemp: {
        id: null,
        name: '',
        flowId: this.flowId,
        sort: 0,
        type: ''
      },
      dialogFormVisible: false,
      listLoading: false,
      list: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      GetFlowBindInfoList(this.flowId).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    resetFormData() {
      this.fromTemp = {
        id: null,
        name: '',
        flowId: this.flowId,
        sort: 0,
        type: ''
      }
      this.dialogFormVisible = true
    },
    deleteBindInfo(bindId) {
      this.$confirm('确认要解除绑定吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        DeleteFlowBindInfo(bindId).then(() => {
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    selectMenuData(row) {
      SaveFlowBindInfo({
        flowId: this.flowId,
        menuId: row.id
      }).then(() => {
        this.getList()
        this.$nextTick(() => {
          this.dialogFormVisible = false
        })
      })
    }
  }
}
</script>
